package com.starblink.home.ui.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.urllink.PullUpPageUtils
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.util.log.YYLogUtils
import com.starblink.home.R
import com.starblink.rocketreserver.fragment.ResourceF
import com.youth.banner.adapter.BannerAdapter
import java.util.ArrayList

/**
 * @author dyhpc
 */
class HomeBannerAdapter(mDatas: ArrayList<ResourceF>, var context: Activity) :
    BannerAdapter<ResourceF, HomeBannerAdapter.BannerViewHolder>(mDatas) {
    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.item_home_feeds_img_banner, parent, false)
        return BannerViewHolder(view)
    }


    inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var img: AppCompatImageView

        init {
            img = view.findViewById(R.id.b_img)
        }
    }

    override fun onBindView(holder: BannerViewHolder, data: ResourceF, position: Int, size: Int) {
        ImageUtils.loadImage(
            context,
            data.mainPic,
            holder.img,
            com.starblink.basic.style.R.drawable.one_page_skeleton_7,
            ImageUtils.errorImage
        )
        holder.img.setOnClickListener {

            YYLogUtils.e("首页banner ---》${data.jumpUrl}")
            data.jumpUrl?.let {
                PullUpPageUtils.routePageIntercept4Url(
                    activity = context,
                    url = it
                )
            }
        }
        holder.img.trackData(
            page = SpmPageDef.Home080Page_10164,
            element = SpmElementDef.HomeNewBannerSwiper_33008,
            exposeTime = 0f,
//            businessSuffixID = businessSuffixId(
//                beanStr = "ResourceF",
//                id = data.id.toString()
//            )
            businessSuffixID = businessSuffixId(data)
        )
    }
}